<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
	<meta name="viewport" content="user-scalable=no,viewport-fit=cover" />
	<link href="./css/baseCss.css" rel="stylesheet" type="text/css"/>
	<link href="./base/layer.css" rel="stylesheet" type="text/css"/>
	<!-- <link href="./base/articlecon.css" rel="stylesheet" type="text/css"/> -->
	<link href="./base/base2.css" rel="stylesheet" type="text/css"/>
	<link rel="stylesheet" type="text/css" href="./css/couponMy.css">

	<script type="text/javascript" src="./base/rem.js"></script>
	<script type="text/javascript" src="./base/iscroll.js"></script>

	<style type="text/css">
		
	</style>
	<title>优惠券</title>
</head>
<body>
	<div>
		<header class="allHeader">
            <div class="headerReturn" onclick="window.history.go(-1);"><img class="imgBai" src="./img/nav_bake.png"></div>
            <span class="addOreditor">优惠券</span>
        </header>
		<section>
			<div class="coupon_sw">
				<div  class="coupon_sw_current" onclick="statusSW(1,this);">未使用</div><div onclick="statusSW(2,this);">已使用</div><div onclick="statusSW(3,this);">已过期</div>
			</div>
			<div id="couponInc">
				<ul class='couponList'>
					<li class='couponList_eve'>
						<div class='couponList_eveM'>
							<div class='couponList_eveM_big'>10</div>
							<div>
								<div class='couponList_eveM_small'>元优惠券</div>
								<div>满10元使用</div>
							</div>
						</div>
						<ul class='couponList_dot'>
							<li></li><li></li><li></li><li></li><li></li><li></li><li></li>
						</ul>
						<div class='couponList_eveT'>
							<div class='couponList_eveT_tit'>呵呵呵呵呵或</div>
							<div class='couponList_eveT_content'>
								<div class='couponList_eveT_conLeft'>
									<div>领取人数/总量:</div>
									<div>100人/1000张</div>
								</div>
								<div class='couponList_eveT_conRight'>
									<div>已使用:</div>
									<div>1000张</div>
								</div>
							</div>
						</div>
					</li>
				</ul>
			</div>
			
		</section>
	</div>
	
</body>

<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript" src="./js/httpUnit.js"></script>
<script type="text/javascript">
	selCoupon();
	//检测是否选择优惠券
	function selCoupon(){
		var Request = GetRequest();
		var select = decodeURI(Request['select']);
		if(select=='true'){
			var satisfyMoney = decodeURI(Request['satisfyMoney']);
			var isOverlay = decodeURI(Request['isOverlay']);
			$(".coupon_sw").css("display","none");
			userCouponList(1,false,satisfyMoney,isOverlay);
		}else{
			userCouponList(1,false);
		}
	}

	
	//状态切换
	function statusSW(status,that){
		$(that).addClass("coupon_sw_current").siblings().removeClass("coupon_sw_current");
		
		var isRefresh = false;
		if(that==undefined||that==null){
			isRefresh= true;
		}
		userCouponList(status,isRefresh);
	}
	
	//优惠券-用户优惠券列表
	function userCouponList(status,isRefresh,satisfyMoney,isOverlay){
		var page = 0;
		if(isRefresh==true){
			page = Math.ceil($("#couponInc .couponList_eve").length/10);
		}
		var size = 30;
		var sort = "o.created_date desc";
		
		if(satisfyMoney==null||satisfyMoney==undefined||satisfyMoney==""){
			var obj = JSON.stringify({
	        	"status":status
	    	});
		}else{
			var Request = GetRequest();
			var restaurantId = decodeURI(Request['restaurantId']);
			if(isOverlay==2){
				isOverlay="";
			}
			var obj = JSON.stringify({
	        	"status":status,
	        	"satisfyMoney":satisfyMoney,
	        	"isOverlay":isOverlay,
	        	"restaurantId":restaurantId
	    	});
		}
	    
	    httpHelperpost("/api/userCoupons/userCouponList?page="+page+"&size="+size+"&sort="+sort, obj, function(isSuccess, data) {
	        if (isSuccess == true) {
	            var response = data.responseText;
	            var value = JSON.parse(response);
	                
	            console.log(response);
	            
	            if(value.code == 0){
	                console.log("查询成功");
	                var html = "";
       //  	     	if(value.data.content==""){
       //  	     		html = "<div class='coupon_no'>"
       //  	     				+"<div><img src='./img/couNo.png'></div>"
							// 	+"<div>当前没有优惠券</div>"
							// 	+"<div class='coupon_no_small'>点击<span>右上角添加按钮</span>设置</div>"
							// +"</div>";
       //  	     	}else{
        	     	if(status==1){
        	     		html = "<ul class='couponList'>";
        	     	}else{
        	     		html = "<ul class='couponList couponList_fail'>";
        	     	}
        	     	
        	     		for(var i=0;i<value.data.length;i++){
        	     			html+="<li class='couponList_eve' data-id='"+value.data[i].id+"' data-isOverlay="+value.data[i].isOverlay+">"
								+"<div class='couponList_eveM'>"
									+"<div class='couponList_eveM_big'>"+numisNull(value.data[i].reduceMoney)+"</div>"
									+"<div>"
										+"<div class='couponList_eveM_small'>元优惠券</div>"
										+"<div>满<span class='satisfyMoney'>"+numisNull(value.data[i].satisfyMoney)+"</span>元使用</div>"
									+"</div>"
								+"</div>"
								+"<ul class='couponList_dot'>"
									+"<li></li><li></li><li></li><li></li><li></li><li></li><li></li>"
								+"</ul>"
								+"<div class='couponList_eveT'>"
									+"<div class='couponList_eveT_tit'>"+value.data[i].couponName+"</div>"
									// +"<div class='couponList_eveT_content'>"
									// 	+"<div class='couponList_eveT_conLeft'>"
									// 		+"<div>领取人数/总量:</div>"
									// 		+"<div>"+numisNull(value.data[i].receiveNumber)+"人/"+numisNull(value.data[i].amount)+"张</div>"
									// 	+"</div>"
									// 	+"<div class='couponList_eveT_conRight'>"
									// 		+"<div>已使用:</div>"
									// 		+"<div>"+numisNull(value.data[i].alreadyUsed)+"张</div>"
									// 	+"</div>"
									// +"</div>"
								+"</div>"
							+"</li>"
        	     		}
        	     				
					html+="</ul>";
        	     	// }

        	     	if(isRefresh==true){
        	     		$("#couponInc").append(html);
        	     	}else if(isRefresh==false){
        	     		$("#couponInc").html(html);
        	     	}
        	     	if(value.data==""){

        	     	}else{
        	     		dropRefresh(status);
        	     	}

        	     	//如果是选择优惠券页面的逻辑
        	     	if(satisfyMoney==null||satisfyMoney==undefined||satisfyMoney==""){
						
					}else{
						$(".couponList_eve").click(function(){
							var selectCoupon = {id:$(this).attr("data-id"),couponName:$(this).find(".couponList_eveT_tit").text(),satisfyMoney:$(this).find(".satisfyMoney").text(),reduceMoney:$(this).find(".couponList_eveM_big").text(),isOverlay:isOverlay};
							sessionStorage.setItem("selectCoupon",JSON.stringify(selectCoupon));

							window.history.go(-1);
						});
					}
	            }else if(value.code==401){
	            	alert(value.message);
	            	window.location.href='./index.html';
	            }else{
	                alert(value.message);
	                console.log("查询失败");
	                
	            }
	        }else {}
	    });
	}

	

	//下拉刷新
    function dropRefresh(status){
    	
    	$(window).scroll(function() {
			var scrollTop = $(document).scrollTop();//滚动条距离页面顶部距离
			var offsetHeight = $(document).height();//当前页面高度(包括滚动页面)
			var clientHeight = $(this).height();//当前页面高度(不包括滚动页面)
			var bottomHeight = scrollTop-(offsetHeight-clientHeight);
			
			if(bottomHeight<2&&bottomHeight>-2){
				// console.log(bottomHeight);
				userCouponList(status,true);
				
				$(window).unbind("scroll");		
			}
		});
    }

	//数字判空函数
	function numisNull(num){
		if(num==""||num==null||num==undefined){
			return 0;
		}else{
			return num;
		}
	}

	//获取url中"?"符后的字串
	function GetRequest() {
    	var url = location.search; //获取url中"?"符后的字串   
    	
    	var theRequest = new Object();
    	if (url.indexOf("?") != -1) {
    	  var str = url.substr(1);
    	  
    	  strs = str.split("&");
    	  
    	  for (var i = 0; i < strs.length; i++) {
    	    
    	    theRequest[strs[i].split("=")[0]] = strs[i]
    	        .split("=")[1];
    	  }
    	}
    	return theRequest;
  	}
</script>
</html>